package com.mh.mh.netty.closechannel;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * 通用handler，处理IO事件
 * SimpleChannelInboundHandler 是客户端用的
 * @author SG423
 */
@ChannelHandler.Sharable// Sharable 为了线程安全
public class HandlerClientHelloClose extends SimpleChannelInboundHandler<ByteBuf> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("连上服务器了");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        /***
         * 处理接收到的消息
         */
        System.out.println("接收到的消息"+byteBuf.toString(CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        /**
         * 处理IO异常
         */
        cause.printStackTrace();
        ctx.close();
    }
}
